<template>
    <view
        class="container chopDetail"
        :style="{ background: loading ? '#fff' : '#bb0024' }"
    >
        <chopSkeleton v-if="loading"></chopSkeleton>
        <!-- <image
            class="bg-img"
            src="https://jiumiaodao.oss-cn-guangzhou.aliyuncs.com/upload/2024-03-29/20240329182530d54e20513.jpg"
        ></image> -->
        <!-- 外部容器 -->
        <view
            v-else
            class="outside_con"
        >
            <!-- 内容容器 -->
            <view class="content">
                <view style="position: relative">
                    <image
                        class="chop_detail_img"
                        :src="productDetail.product.image[0].file_path"
                    ></image>
                    <view
                        v-if="
                            taskDetail.product_stock == 0 && taskInfo.is_creater
                        "
                        class="aa"
                    >
                        已抢完
                    </view>
                </view>
                <view class="productDetail">
                    <view>
                        <view class="productName">
                            {{ productDetail.product.product_name }}
                        </view>
                        <view class="crat-product-sku u-line-1">
                            {{ taskDetail.product_attr }}
                        </view>
                    </view>
                    <view class="seckill-activity">
                        <text class="seckill-activity-ele">最低</text>
                        <text class="sellMoney_small">
                            {{ taskDetail.bargain_price }}
                        </text>
                    </view>
                </view>
            </view>
            <!-- <countdown
                v-if="!taskDetail.is_end && taskDetail.status === 0" -->
            <countdown
                v-if="!taskDetail.is_end"
                class="bargain_countdown"
                ref="countdown"
                @returnVal="countdownReturnVal"
                :config="countdownConfig"
                color="#e80d0d"
            ></countdown>
            <!-- <view
                v-else
                class="countdown_end"
                >当前助力已结束</view
            > -->
            <view v-if="taskDetail.status === 0 && taskInfo.is_creater">
                <view class="product-bargin-price">
                    已助力<text class="import_text">{{
                        taskDetail.cut_money
                    }}</text
                    >元，距离最低还差<text class="import_text">{{
                        taskDetail.surplus_money
                    }}</text
                    >元
                </view>
                <!-- 进度条 -->
                <view class="chop-progress">
                    <u-line-progress
                        height="16"
                        :percent="taskDetail.bargain_rate"
                        active-color="#e80d0d"
                        striped-active
                    ></u-line-progress>
                </view>
                <view
                    v-if="activeDetail.conditions === 1"
                    class="cut_text"
                    >助力到{{ taskDetail.bargain_price }}可下单</view
                >
            </view>
            <view class="button_container">
                <view
                    class="btn_con"
                    v-if="taskInfo.is_creater"
                >
                    <!-- 当前用户是否助力过 -->
                    <!-- <b-u-button-theme
                        v-if="
                            !taskInfo.is_cut &&
                            taskDetail.status === 0 &&
                            !taskDetail.is_end &&
                            !isDone &&
                            taskDetail.surplus_money !== '0.00'
                        "
                        height="80rpx"
                        width="100%"
                        @click.stop="toCut"
                    >
                        立即助力
                    </b-u-button-theme> -->
                    <u-button
                        v-if="taskDetail.status === 0"
                        shape="circle"
                        open-type="share"
                        :hair-line="false"
                        :custom-style="{
                            background: $setting.colorTheme(),
                            width: '100%',
                            height: '80rpx',
                            color: '#fff',
                            border: '1px solid ' + $setting.colorTheme(),
                            fontSize: '28rpx',
                        }"
                        @click.stop="inviteFriend"
                        >邀请新用户助力</u-button
                    >
                    <!-- 助力非失败状态 -->
                    <!-- conditions购买条件(0:助力中可购买，1:助力到底价可购买 ) -->
                    <view v-if="taskDetail.product_stock == 0">
                        <b-u-button-grey
                            v-if="taskDetail.is_buy === 1"
                            height="80rpx"
                            width="100%"
                            >已下单</b-u-button-grey
                        >
                        <b-u-button-grey
                            v-else
                            height="80rpx"
                            width="100%"
                        >
                            已抢完
                        </b-u-button-grey>
                    </view>

                    <view v-else>
                        <view v-if="taskDetail.is_buy === 0">
                            <b-u-button-theme
                                v-if="
                                    activeDetail.conditions == 0 &&
                                    taskDetail.is_buy === 0
                                "
                                height="80rpx"
                                width="100%"
                                @click.stop="toBuy(taskDetail)"
                            >
                                立即{{ taskDetail.actual_price }}下单
                            </b-u-button-theme>
                            <!-- 是否已助力到底价(0否 1是) -->
                            <b-u-button-theme
                                v-if="
                                    activeDetail.conditions === 1 &&
                                    taskDetail.is_floor &&
                                    taskDetail.status === 1
                                "
                                height="80rpx"
                                width="100%"
                                @click.stop="toBuy(taskDetail)"
                            >
                                立即{{ taskDetail.actual_price }}下单
                            </b-u-button-theme>
                        </view>
                        <b-u-button-grey
                            v-else
                            height="80rpx"
                            width="100%"
                        >
                            已下单
                        </b-u-button-grey>
                    </view>
                </view>
                <!-- 好友助力 -->
                <view
                    class="btn_con"
                    v-else
                >
                    <!-- 助力状态: 进行中 -->
                    <!-- <view
                        v-if="
                            taskDetail.status === 0 &&
                            !taskDetail.is_end &&
                            !isDone
                        "
                    > -->
                    <view v-if="!taskDetail.is_end && taskDetail.is_buy === 0">
                        <!-- 未助力并且当前助力未到期 -->
                        <b-u-button-theme
                            v-if="!taskInfo.is_cut"
                            height="80rpx"
                            width="100%"
                            @click.stop="toCut"
                        >
                            立即助力
                        </b-u-button-theme>
                    </view>
                    <b-u-button-grey
                        v-else
                        height="80rpx"
                        width="100%"
                    >
                        立即助力
                    </b-u-button-grey>
                    <b-u-button-theme
                        height="80rpx"
                        width="100%"
                        @click.stop="play"
                    >
                        我也要领
                    </b-u-button-theme>
                </view>
            </view>
            <!-- 助力记录 -->

            <view class="tabs-list">
                <u-tabs
                    :list="tabsList"
                    :is-scroll="false"
                    :current="tabsCurrent"
                    bg-color="#ffe4b7"
                    :active-color="$setting.colorTheme()"
                    @change="tabsChange"
                ></u-tabs>
                <view v-if="tabsCurrent === 0">
                    <view
                        class="user_cut_list"
                        v-if="helpList.length"
                    >
                        <view
                            class="user_list"
                            v-for="(item, index) in helpList"
                            :key="index"
                        >
                            <view class="friend_avatarUrl">
                                <image
                                    class="friend_avatarUrl"
                                    :src="
                                        item.user.avatarUrl ||
                                        $setting.avatarUrl()
                                    "
                                ></image>
                            </view>
                            <view class="right">
                                <view>{{ item.user.nickName }}</view>
                                <view class="cut_money"
                                    >助力{{ item.cut_money }}元</view
                                >
                            </view>
                        </view>
                    </view>
                    <view
                        v-else
                        :style="{ margin: '30rpx 0' }"
                    >
                        <u-empty
                            text="邀请好友助力"
                            mode="data"
                        ></u-empty>
                    </view>
                </view>
            </view>
        </view>
        <u-popup
            class="showPopup"
            v-model="showPop"
            mode="center"
            :mask-close-able="false"
            border-radius="20"
        >
            <view class="pop_container">
                <!-- <image class="active_end_img" :src="$setting.imageUrl()+'static/active_end.png'" mode="widthFix"></image> -->
                <image
                    class="active_end_img"
                    :src="$setting.imageUrl() + 'static/active_end.png'"
                    mode="widthFix"
                ></image>
                <view class="end_back">
                    <view class="end_text">活动已经结束！</view>
                    <view
                        class="popHomePage"
                        @click="homePageFun()"
                    >
                        了解更多精彩活动
                    </view>
                </view>
            </view>
        </u-popup>
        <view
            class="backActive"
            @click="play"
            >返回活动</view
        >
    </view>
</template>

<script>
import chopSkeleton from '@/pages/component/skeleton/chopSkeleton.vue';
export default {
    components: { chopSkeleton },
    data() {
        return {
            loading: true,
            showPop: false,
            userInfo: {},
            options: {},
            productDetail: {},
            taskDetail: {},
            activeDetail: {},
            taskInfo: {},
            tabsCurrent: 0,
            helpList: [],
            isDone: false,
            countdownStatus: '',
            countdownConfig: {
                endstamp: '',
                startstamp: '',
                title: '距截止还剩：',
                type: 'text',
            },
            tabsList: [
                {
                    id: 'record',
                    name: '助力记录',
                },
            ],
            share: {
                title: '',
                imageUrl: '',
            },
        };
    },
    watch: {
        isDone(val) {
            if (val && this.taskDetail.status !== 2) {
                this.getBargainTaskDetail(this.options);
            }
        },
    },
    async onLoad(options) {
        this.loading = true;
        this.options = options;
        await this.$onLaunched;
        await this.getHttp(options);
    },
    async onShow() {
        await this.$onLaunched;
        this.getUserInfo();
    },
    mounted() {},
    onShareAppMessage() {
        return {
            title: this.activeDetail?.share_title ?? '助力专区',
            imageUrl: this.activeDetail?.share_img_url?.file_path ?? '',
            path:
                '/pages/active/chopDetail/chopDetail?bargain_product_id=' +
                this.options.bargain_product_id +
                '&bargain_task_id=' +
                this.options.bargain_task_id,
        };
    },
    methods: {
        getHttp(params = {}) {
            this.getUserInfo();
            this.getBargainTaskDetail(params);
            this.getConfigDataDetail();
        },
        async getUserInfo() {
            try {
                const res = await uni.$u.api.userInfo();
                this.userInfo = res.userInfo;
                console.log(this.userInfo);
            } catch (error) {}
        },
        async getBargainTaskDetail(params) {
            try {
                const { bargain_task_id } = params;
                const res = await uni.$u.api.getBargainTaskDetail({
                    bargain_task_id,
                });
                this.taskInfo = res;
                this.taskDetail = res?.task ?? {};
                this.activeDetail = res?.active ?? {};
                console.log(this.activeDetail);
                this.helpList = res?.help_list ?? [];
                this.productDetail = res?.product ?? [];
                this.countdownConfig.endstamp = this.taskDetail.end_time;
                // 当前获取到的时间戳为16位
                this.countdownConfig.startstamp = this.taskDetail.start_time;
                this.loading = false;
            } catch (error) {
                this.loading = false;
            }
        },
        countdownReturnVal(val) {
            // 0：活动进行中，1：活动时间还没到，2：活动过期
            this.countdownStatus = val;
            this.isDone = val === 2;
        },
        inviteFriend() {},
        async toCut() {
            try {
                await this.getUserInfo();
                if (uni.$u.test.isEmpty(this.userInfo.mobile)) {
                    uni.navigateTo({
                        url: '/pages/login/login',
                    });
                    return;
                }
                await uni.$u.api.postUserCut(this.options);
                this.getBargainTaskDetail(this.options);
            } catch (error) {}
        },
        toBuy() {
            uni.navigateTo({
                url:
                    '/pages/cart/settle/settle?bargain_task_id=' +
                    this.taskDetail.bargain_task_id +
                    '&bargain_product_id=' +
                    this.taskDetail.bargain_product_id +
                    '&bargain_product_sku_id=' +
                    this.taskDetail.bargain_product_sku_id +
                    '&order_type=bargain&product_sku_id=' +
                    this.taskDetail.product_sku_id,
            });
        },
        tabsChange(index) {
            this.tabsCurrent = index;
        },
        play() {
            uni.navigateTo({
                url: '/pages/active/chop/chop',
            });
        },
        getConfigDataDetail() {
            let params = {
                page_type: 'bargainDetail',
            };
            uni.$u.api.getConfigDataDetail(params).then((res) => {
                let Detail = res.data;
                this.share.title = Detail.content;
                this.share.imageUrl = Detail.image;
            });
        },
        homePageFun() {
            uni.switchTab({
                url: '/pages/index/index',
            });
        },
    },
};
</script>

<style lang="scss" scoped>
.container {
    box-sizing: border-box;
    padding: 25rpx;
    // background-image: url(https://jiumiaodao.oss-cn-guangzhou.aliyuncs.com/upload/2024-03-29/20240329182530d54e20513.jpg);
    background-repeat: no-repeat;
    background-size: 100% 100vh;
    background-color: #bb0024;
    position: relative;
}
.bg-img {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    top: 0;
    height: 100%;
    width: 100%;
}
.outside_con {
    position: relative;
    box-sizing: border-box;
    min-height: calc(100vh - 50rpx);
    padding: 20rpx;
    background-color: #ffffff;
    border-radius: 24rpx;
}
.content {
    display: flex;
    padding: 12rpx;
    background-color: #f6f6f6;
    border-radius: 8rpx;
}
.chop_detail_img {
    width: 250rpx;
    height: 250rpx;
    border-radius: 16rpx;
}
.crat-product-sku {
    font-size: 24rpx;
    color: #999999;
    margin-bottom: 12rpx;
}
.productDetail {
    display: flex;
    flex: 1;
    justify-content: space-around;
    flex-direction: column;
    margin-left: 20rpx;
}
.productName {
    overflow: hidden;
    -webkit-line-clamp: 2;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-box-orient: vertical;
}

.bargain_countdown {
    display: flex;
    justify-content: center;
    margin: 40rpx;
    font-size: 32rpx;
}
.product-bargin-price {
    text-align: center;
    margin: 20rpx 0px;
    font-size: 32rpx;
    .import_text {
        font-size: 38rpx;
    }
}
.import_text {
    color: #e80d0d;
    font-weight: bolder;
}
.cut_text {
    text-align: center;
    font-weight: bolder;
    color: #e80d0d;
    margin: 28rpx;
}
.button_container {
    width: 100%;
    display: flex;
    justify-content: center;
    /deep/button {
        margin-bottom: 28rpx;
    }
    .btn_con {
        width: 100%;
    }
}
.product-item-old-price {
    font-size: 24rpx;
    text-decoration: line-through;
    color: #999999;
    margin-left: 20rpx;
}
.friend_avatarUrl {
    width: 80rpx;
    height: 80rpx;
    border-radius: 50%;
    background-color: #d8d8d8;
}
.user_cut_list {
    padding: 30rpx 0;
    box-sizing: border-box;
}
.user_list {
    display: flex;
    margin-bottom: 20rpx;
    .right {
        flex: 1;
        margin-left: 10rpx;
        display: flex;
        justify-content: space-between;
    }
}
.u-tabs {
    border-radius: 18rpx 18rpx 0px 0px !important;
}
.countdown {
    font-size: 32rpx;
    color: #e80d0d;
}
.tabs-list {
    margin-top: 20rpx;
}

.countdown_end {
    display: flex;
    justify-content: center;
    margin: 40rpx;
    font-size: 32rpx;
    color: #e80d0d;
}
.seckill-activity {
    font-size: 28rpx;
    padding: 0 4rpx;
    margin: 10rpx 0;
    color: #bb0024;
    display: flex;
    align-items: center;
    border-radius: 8rpx;
    border: 1px solid #bb0024;
    display: inline-block;
    width: fit-content;
}
.seckill-activity-ele {
    display: inline-block;
    padding: 0 4rpx;
    background-image: linear-gradient(to right, #bb0024, #fa0032);
    color: #fff;
    margin: 0 10rpx 0 -4rpx;
    border-radius: 7rpx 0 0 7rpx;
}
.chop-progress {
    margin: 20rpx 0;
}
.aa {
    font-size: 55rpx;
    font-weight: bold;
    position: absolute;
    left: 20%;
    top: 30%;
    color: #e80d0d;
    letter-spacing: 8rpx;
    transform: rotate(-45deg);
}
.pop_container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 450rpx;
    position: relative;
    padding: 30rpx 30rpx 40rpx;
}
.active_end_img {
    width: 160rpx;
}
.end_back {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
}
.end_text {
    color: #666;
    font-size: 22rpx;
    padding: 16rpx;
}
.backActive {
    height: 180rpx;
    width: 50rpx;
    position: fixed;
    box-sizing: border-box;
    padding: 8rpx;
    // border: 1px solid #bb0024;
    border: 1px solid;
    // background-color: white;
    bottom: 20vh;
    right: 5vw;
    border-radius: 8rpx;
    z-index: 99;
}
</style>
<style lang="scss">
.chopDetail {
    /deep/.countdown {
        font-size: 32rpx;
        color: #e80d0d;
    }
}
</style>
